package com.starbucks.opencase.function;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daSession;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMTip;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.Set;

@Component
public class funSession {
    /**
     * 发送消息
     * @param id
     * @param message
     * @return
     */
    public boolean sendMessage(String id, JsonNode message){
        try {
            if(id==null || id.isEmpty())return false;
            WebSocketSession session = query(id);
            if(session==null)return false;
            session.sendMessage(new TextMessage(message.toString()));
            System.out.println("["+ String.format("%29s",LocalDateTime.now().toString()).replaceAll(" ","0") +"]["+id+"][server]\t"+message.toString());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 获取客户端
     * @param id
     * @return
     */
    public WebSocketSession query(String id){
        for(WebSocketSession se: daSession.clients)if(se!=null && se.getId().equals(id))return se;
        return null;
    }

    /**
     * 关闭客户端
     * @param id
     */
    public void close(String id){
        try {
            WebSocketSession se=query(id);
            if(se!=null)se.close();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        removeClient(id);
        removeClientUser(id);
    }

    /**
     * 移除客户端
     * @param id
     */
    public void removeClient(String id){
        for(int i=0;i< daSession.clients.size();i++){
            if(daSession.clients.get(i)!=null && daSession.clients.get(i).getId().equals(id)){
                daSession.clients.remove(i);
                return;
            }
        }
    }

    /**
     * 移除客户端与玩家的绑定
     * @param id
     */
    public void removeClientUser(String id){
        daSession.client_users.remove(id);
    }

    /**
     * 绑定客户端与玩家
     * @param id
     * @param uid
     */
    public void bindClientUser(String id,String uid){
        daSession.client_users.put(id,uid);
    }

    /**
     * 解绑客户端与玩家
     * @param sid
     */
    public void unbindClientUser(String sid){
        daSession.client_users.remove(sid);
    }
    /**
     * 该玩家是否在线
     * @param uid
     */
    public boolean isOnlineOfUId(String uid,String id,ObjectNode on){
        boolean ok= daSession.client_users.containsValue(uid);
        if(!ok){
            on.put(enumCMKey.tip.name(), enumCMTip.not_online.getTip());
            sendMessage(id,on);
        }
        return ok;
    }
    public boolean isOnlineOfUId(String uid){
        return daSession.client_users.containsValue(uid);
    }
    /**
     * 该玩家是否在线
     * @param id 客户端id
     */
    public boolean isOnlineOfId(String id, ObjectNode on){
        boolean ok= daSession.client_users.containsKey(id);
        if(!ok && on!=null){
            on.put(enumCMKey.tip.name(), enumCMTip.not_online.getTip());
            on.put(enumCMKey.status.name(), false);
            sendMessage(id,on);
        }
        return ok;
    }

    /**
     * 获取uid
     * @param id
     * @return
     */
    public String getUIdOfId(String id){
        return daSession.client_users.get(id);
    }
    public String getIdOfUId(String uid){
        for(String u:daSession.client_users.keySet())if(daSession.client_users.get(u).equals(uid))return u;
        return "";
    }
    public int getOnlineNum(){
        return daSession.client_users.size();
    }
    public String randomUID(String uid){
        Set<String> as= daSession.client_users.keySet();
        as.remove(uid);
        return daSession.client_users.get(as.toArray()[new Random().nextInt(as.size())]);
    }
}
